package cn.jeeweb.web.modules.sys.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.query.data.Queryable;
import cn.jeeweb.common.query.utils.QueryableConvertUtils;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.sys.entity.UiControlRule;
import cn.jeeweb.web.modules.sys.service.IUiControlruleService;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 *
 */
@RestController
@RequestMapping("/ui/controlrule")
@ViewPrefix("modules/UipackageInfo")
//@RequiresPathPermission("ui:packageinfo")
@Log(title = "ui_controlrule")
public class UiControlRuleController extends BaseBeanController<UiControlRule> {
	@Autowired
	private IUiControlruleService uiControlruleService;

	@PostMapping("add")
	@Log(logType = ModuleActionType.INSERT)
//	@RequiresMethodPermissions("add")
	public Response add(UiControlRule entity, BindingResult result,
						HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		uiControlruleService.insert(entity);
		return Response.ok("添加成功");
	}

	@PostMapping("{id}/update")
	@Log(logType = ModuleActionType.UPDATE)
//	@RequiresMethodPermissions("update")
	public Response update(UiControlRule entity, BindingResult result,
						   HttpServletRequest request, HttpServletResponse response) {
		// 验证错误
		this.checkError(entity,result);
		uiControlruleService.insertOrUpdate(entity);
		return Response.ok("更新成功");
	}

	@PostMapping("{id}/delete")
	@Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
	public Response delete(@PathVariable("id") String id) {
		uiControlruleService.deleteById(id);
		return Response.ok("删除成功");
	}

	/**
	 * 根据页码和每页记录数，以及查询条件动态加载数据
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "getList", method = { RequestMethod.GET, RequestMethod.POST })
	@Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
	public void ajaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
		EntityWrapper<UiControlRule> entityWrapper = new EntityWrapper<>(entityClass);
		propertyPreFilterable.addQueryProperty(new String[]{"id","moduleId","eventType","handleType","callBackParams","businessType","referenceContext","description"});
		// 预处理
		QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
		SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
		// 预处理
		Page pageBean = uiControlruleService.selectPage(PageRequest.getPage(),entityWrapper);
		FastJsonUtils.print(pageBean);
	}

}